//
//  LRHttpManager.m
//  Anyair
//
//  Created by karlcool on 14-5-24.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "LRHttpManager.h"

@implementation LRHttpManager


static LRHttpManager *httpManager;

+(LRHttpManager *)createHttpManager{
    @synchronized(self)
    {
        if (httpManager == nil) {
            httpManager=[[self alloc] init];
        }
    }
    return httpManager;
}

#pragma mark - 登陆

- (NSDictionary*)logInUserByPhone:(NSString*)phone AndPassword:(NSString*)password {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/member/login",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"pwd\":\"%@\"}",phone,password];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"登陆返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - 注册
- (NSDictionary*)signInUserByPhone:(NSString*)phone AndPassword:(NSString*)password AndVerifycode:(NSString*)vCode{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/member/registe",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"pwd\":\"%@\",\"verifycode\":\"%@\",\"ostype\":\"0\"}",phone,password,vCode];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"注册返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - 发送验证码 0注册1找回
- (NSDictionary*)sendAuthToPhone:(NSString*)phone AndType:(NSString*)type {
    
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/member/getverify",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"verifytype\":\"%@\"}",phone,type];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"发送验证码返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];

        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
    
}

#pragma mark - 重设密码
- (NSDictionary*)resetPasswordByPhone:(NSString*)phone AndNewPassword:(NSString*)newPassword AndVerifycode:(NSString*)vCode {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/member/resetpwd",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"npwd\":\"%@\",\"verifycode\":\"%@\"}",phone,newPassword,vCode];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"重设密码返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - 环境质量提示
- (NSDictionary*)getHintByPhone:(NSString*)phone {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\"}",phone];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"环境质量提示返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - 目的地数据查询
- (NSDictionary*)queryPlaceByPhone:(NSString*)phone AndLongitude:(NSString*)longitude AndLatitude:(NSString*)latitude AndType:(NSString*)type {
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/check/query",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"querytype\":\"%@\"}",phone,longitude,latitude,type];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"目的地数据查询返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"目的地数据查询返回%@",resposeDic);
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
    
}

#pragma mark - 周边数据查询
- (NSDictionary*)queryPOIByPhone:(NSString*)phone AndLongitude:(NSString*)longitude AndLatitude:(NSString*)latitude {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/check/getPoiDetails",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\"}",phone,longitude,latitude];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"周边数据查询返回%@",resposeDic);
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - 历史数据查询
- (NSDictionary*)queryHistoryByPhone:(NSString*)phone AndTime:(NSString*)time AndType:(NSString*)type AndPage:(NSString*)page {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/check/history",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    NSString *str;
    if (time.length==0) {
        str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"querytype\":\"%@\",\"page_index\":\"%@\"}",phone,type,page];
    } else {
        str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"time\":\"%@\",\"querytype\":\"%@\",\"page_index\":\"%@\"}",phone,time,type,page];
    }
    
    
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"历史数据查询返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - 甲醛单次检测数据上传
- (NSDictionary*)updateHCHOByPhone:(NSString*)phone AndLongitude:(NSString*)longitude AndLatitude:(NSString*)latitude AndCelsius:(NSString*)celsius AndDampness:(NSString*)dampness AndAddress:(NSString*)address AndPlacediy:(NSString*)placediy AndHCHO:(NSString*)hcho {
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/check/savehcho",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"celsius\":\"%@\",\"dampness\":\"%@\",\"address\":\"%@\",\"placediy\":\"%@\",\"hcho\":\"%@\"}",phone,longitude,latitude,celsius,dampness,address,placediy,hcho];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"甲醛上传返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - CO2单次检测数据上传
- (NSDictionary*)updateCO2ByPhone:(NSString*)phone AndLongitude:(NSString*)longitude AndLatitude:(NSString*)latitude AndCelsius:(NSString*)celsius AndDampness:(NSString*)dampness AndAddress:(NSString*)address AndPlacediy:(NSString*)placediy AndCO2:(NSString*)co2 AndCheckType:(NSString*)checkType {
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/check/saveco2",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"celsius\":\"%@\",\"dampness\":\"%@\",\"address\":\"%@\",\"placediy\":\"%@\",\"Co2\":\"%@\",\"checktype\":\"%@\"}",phone,longitude,latitude,celsius,dampness,address,placediy,co2,checkType];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"CO2上传返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
}

#pragma mark - PM单次检测数据上传
- (NSDictionary*)updatePMByPhone:(NSString*)phone AndLongitude:(NSString*)longitude AndLatitude:(NSString*)latitude AndCelsius:(NSString*)celsius AndDampness:(NSString*)dampness AndAddress:(NSString*)address AndPlacediy:(NSString*)placediy AndPm:(NSString*)pm AndPm2:(NSString*)pm2 AndPm10:(NSString*)pm10 {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/forms/check/savepm25",SERVER_URL]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    
    NSString *str = [NSString stringWithFormat:@"{\"phoneno\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"celsius\":\"%@\",\"dampness\":\"%@\",\"address\":\"%@\",\"placediy\":\"%@\",\"pm\":\"%@\",\"pm2\":\"%@\",\"pm10\":\"%@\"}",phone,longitude,latitude,celsius,dampness,address,placediy,pm,pm2,pm10];
    [request setPostValue:@" " forKey:str];
    [request startSynchronous];
    
    NSError *error = [request error];
    if (!error) {
        NSLog(@"PM上传返回%@",[request responseString]);
        NSDictionary *resposeDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
        return resposeDic;
    }else{
        NSLog(@"%@",error);
        NSDictionary *resposeDic = @{@"code":@"0003"};
        return resposeDic;
    }
    
}
@end
